varying vec3 N;
varying vec4 v;
uniform highp float gamma;
uniform sampler2D texture;
uniform sampler2D glow;
varying vec2 vTexCoord;
varying vec4 vColor;

void main(void) {  
        vec3 L = normalize(gl_LightSource[0].position.xyz - v.xyz);

	// we are in Eye Coordinates, so EyePos is (0,0,0)
        vec3 E = normalize(-v.xyz);
	vec3 R = normalize(-reflect(L,N));  

	//calculate Ambient Term:
	vec4 Iamb = gl_FrontLightProduct[0].ambient;

	//calculate Diffuse Term:
	vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
	Idiff = clamp(Idiff, 0.0, 1.0);

	// calculate Specular Term:
	vec4 Ispec = gl_FrontLightProduct[0].specular
				* pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
	Ispec = clamp(Ispec, 0.0, 1.0);

        // write Total Color scene Emission + Ambient + Diffuse + Specular :
	vec4 c = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec;
	
	// multiply by the color of the texture
        vec4 tex = texture2D(texture, vTexCoord);
        c = c * vec4(tex.r, tex.g, tex.b, 1.0);
	
        // multiply by the color of the vertex
	c = c * vColor;
	
	// apply gamma correction
	c = pow(c, vec4(1.0 / gamma));
	
	// set transparency to the material's diffuse transparency
	c.a = gl_FrontMaterial.diffuse.a;
	 
        // output this color
        gl_FragData[0] = c;

        gl_FragData[1] = vec4(c.r * (1.0-tex.a), c.g * (1.0-tex.a), c.b * (1.0-tex.a), c.a);

        // calculate the depth
//        float ndcDepth =
//            (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
//            (gl_DepthRange.far - gl_DepthRange.near);
//        float clipDepth = ndcDepth / gl_FragCoord.w;
        //gl_FragData[0] = vec4(clipDepth);
        //gl_FragDepth = clipDepth;
        //gl_FragDepth = gl_FragCoord.z;
}
